//---------------------------------------------------------------------------

#include <vcl.h>
#include <mshtml.h>
#pragma hdrstop

#include "MainWnd.h"
#include "MainWF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
   : TForm(Owner)
{
}


//---------------------------------------------------------------------------

void __fastcall TMainForm::SpecBtnClick(TObject *Sender)
{

   if (SpecBtn->Caption==">>") Move2RTmr->Enabled=true;
   else Move2LTmr->Enabled=true;
}
//---------------------------------------------------------------------------

bool SaveHtml (TCppWebBrowser * browser, AnsiString path)
{
   IHTMLDocument2*  Doc=NULL;
   IPersistFile* iPf;
   if (FAILED (browser->Document->QueryInterface (IID_IHTMLDocument2, (void**) &Doc)))  return false;
   if (FAILED (Doc->QueryInterface (IID_IPersistFile, (void**) &iPf)))  return false;
   iPf->Save(WideString (path), 1);
   return true;
}


//---------------------------------------------------------------------------

void __fastcall TMainForm::GetHTMLBtnClick(TObject *Sender)
{
   WebBrowserForm->WebBrowser->Navigate((WideString(UrlEdit->Text)));
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::Move2LTmrTimer(TObject *Sender)
{
   if (Panel4Btns->Width>=177) {Move2LTmr->Enabled=false; SpecBtn->Caption=">>"; return;}
   Panel4Btns->Width+=10;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::Move2RTmrTimer(TObject *Sender)
{
   if (Panel4Btns->Width<=0) {Move2RTmr->Enabled=false; SpecBtn->Caption="<<"; return;}
   Panel4Btns->Width-=10;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::OpenBtnClick(TObject *Sender)
{
   if (OpenDialog->Execute())
      {
         MainRE->Lines->LoadFromFile(OpenDialog->FileName);
      }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::SaveBtnClick(TObject *Sender)
{

   if (SaveDialog->Execute())
   {
      MainRE->Lines->SaveToFile(SaveDialog->FileName+".html");
   }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::MainREChange(TObject *Sender)
{
   StatusBar->Panels->Items[0]->Text=MainRE->Lines->Count;
}

//---------------------------------------------------------------------------




void __fastcall TMainForm::OpenBrowserBtnClick(TObject *Sender)
{
   WebBrowserForm->WebUrlEdit->Text=UrlEdit->Text;
   if (WebBrowserForm->ShowModal()==mrCancel) return;

}
//---------------------------------------------------------------------------

void __fastcall TWebBrowserForm::WebBrowserDownloadComplete(
      TObject *Sender)
{
    if (SaveHtml(WebBrowserForm->WebBrowser, "tmp.html"))
      MainForm->MainRE->Lines->LoadFromFile("tmp.html");
}
